#ifdef __RTTHREAD__

#include <lvgl.h>
#include <rtthread.h>

#define DBG_TAG    "LVGL"
#define DBG_LVL    DBG_INFO
#include <rtdbg.h>

static lv_obj_t *title_celi;
static lv_obj_t *title_raddr;
static lv_obj_t *title_waddr;
static lv_obj_t *data_celi;
static lv_obj_t *data_percent;
static lv_obj_t *data_raddr;
static lv_obj_t *data_waddr;
static lv_obj_t *icon_bat;
static lv_obj_t *icon_charge;
static lv_timer_t* timer;
static lv_timer_t* timer_200ms;

//LV_FONT_DECLARE(lv_font_heiti_32)
LV_FONT_DECLARE(lv_font_songti_32)
void onTimerUpdate(lv_timer_t* timer);
void onTimer200ms(lv_timer_t* timer);

void lv_user_gui_init(void)
{
    static lv_style_t style_font;
    static lv_style_t style_font_16;
    static lv_style_t style_bat;
    static lv_style_t style_charge;
    static lv_style_t data_font;
    
    lv_style_init(&style_font);
    lv_style_set_text_font(&style_font,  &lv_font_songti_32); 
    lv_style_set_text_color(&style_font, lv_color_black());
    
    lv_style_init(&style_font_16);
    lv_style_set_text_font(&style_font_16,  &lv_font_montserrat_14); 
    lv_style_set_text_color(&style_font_16, lv_color_black());
    
    lv_style_init(&style_charge);
    lv_style_set_text_font(&style_charge,  &lv_font_montserrat_14); 
    lv_style_set_text_color(&style_charge, lv_color_make(0x32, 0xcd, 0x32)); 
    
    lv_style_init(&data_font);
    lv_style_set_text_font(&data_font,  &lv_font_montserrat_32); 
    lv_style_set_text_color(&data_font, lv_color_black());
    
    title_celi = lv_label_create(lv_scr_act());
    lv_obj_set_pos(title_celi, 0, 48);
    lv_obj_add_style(title_celi, &style_font, LV_STATE_DEFAULT);
    lv_label_set_text(title_celi, "测力值: ");
    
    title_raddr = lv_label_create(lv_scr_act());
    lv_obj_set_pos(title_raddr, 0, 160 - 16);
    lv_obj_add_style(title_raddr, &style_font, LV_STATE_DEFAULT);
    lv_label_set_text(title_raddr, "读地址: ");
    
    title_waddr = lv_label_create(lv_scr_act());
    lv_obj_set_pos(title_waddr, 0, 320-48-32);
    lv_obj_add_style(title_waddr, &style_font, LV_STATE_DEFAULT);
    lv_label_set_text(title_waddr, "写地址: ");
    
    data_celi = lv_label_create(lv_scr_act());
    lv_obj_add_style(data_celi, &data_font, LV_STATE_DEFAULT);
    lv_obj_set_pos(data_celi, 120, 48);
    lv_label_set_text(data_celi, " ");
    
    data_percent = lv_label_create(lv_scr_act());
    lv_obj_add_style(data_percent, &style_font_16, LV_STATE_DEFAULT);
    lv_obj_set_pos(data_percent, 156, 9);
    lv_obj_set_style_text_align(data_percent, LV_TEXT_ALIGN_RIGHT, 0);
    lv_obj_set_width(data_percent, 36);
    lv_label_set_text(data_percent, "100%");
    
    icon_bat = lv_label_create(lv_scr_act());
    lv_obj_set_pos(icon_bat, 195, 0);
    lv_obj_add_style(icon_bat, &data_font, LV_STATE_DEFAULT);
    lv_label_set_text(icon_bat, LV_SYMBOL_BATTERY_3);
    //img = lv_img_create(lv_scr_act());
    //lv_img_set_src(img, LV_SYMBOL_BATTERY_EMPTY);
    
    icon_charge = lv_label_create(lv_scr_act());
    //lv_obj_set_pos(icon_charge, 146, 9);
    lv_obj_set_pos(icon_charge, 210, 9);
    lv_obj_add_style(icon_charge, &style_charge, LV_STATE_DEFAULT);
    lv_label_set_text(icon_charge, LV_SYMBOL_CHARGE);
    lv_obj_move_foreground(icon_charge);
    lv_obj_move_background(icon_bat);
    
    
    timer = lv_timer_create(onTimerUpdate, 1000, RT_NULL);
    lv_timer_ready(timer);
    
    //timer_200ms = lv_timer_create(onTimer200ms, 500, RT_NULL);
    //lv_timer_ready(timer_200ms);
}

void lv_title_fps(int fps)
{
    lv_label_set_text_fmt(data_celi, "%"LV_PRId32, fps);
}

void onTimer200ms(lv_timer_t* timer)
{
    static int bat_sta;
    
    bat_sta++;
    if (bat_sta >= 5)
    {
        bat_sta = 0;
    }
    switch (bat_sta)
    {
        case 0:
            lv_label_set_text(icon_bat, LV_SYMBOL_BATTERY_EMPTY);
            break;
        case 1:
            lv_label_set_text(icon_bat, LV_SYMBOL_BATTERY_1);
            break;
        case 2:
            lv_label_set_text(icon_bat, LV_SYMBOL_BATTERY_2);
            break;
        case 3:
            lv_label_set_text(icon_bat, LV_SYMBOL_BATTERY_3);
            break;
        case 4:
            lv_label_set_text(icon_bat, LV_SYMBOL_BATTERY_FULL);
            break;
    }   
}

void onTimerUpdate(lv_timer_t* timer)
{
    static int fps = 120;
    
    if (fps >= 0)
    {
        lv_title_fps(fps);
        fps--;
        lv_label_set_text_fmt(data_percent, "%"LV_PRId32"%", fps);
    }
    
    if (fps %2 == 0)
    {
        lv_obj_add_flag(icon_charge, LV_OBJ_FLAG_HIDDEN);
    }
    else
    {
        lv_obj_clear_flag(icon_charge, LV_OBJ_FLAG_HIDDEN);
    }
}

#endif
